//
//  HomeAddModuleCell.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/4.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "HomeAddModuleCell.h"
#import "Masonry.h"
@interface HomeAddModuleCell()
/** titleLabel */
@property (weak, nonatomic) UILabel * titleLabel;
/** imageView */
@property (weak, nonatomic) UIImageView * iconImageView;
/** View */
@property (weak, nonatomic) UILabel * subTitleLabel;

@property (weak, nonatomic) UIButton *deleteButton;

@end
@implementation HomeAddModuleCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addViews];
        [self setupUI];

    }
    return self;
}
- (void)addViews
{
    UILabel * titleLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        //        label.backgroundColor = [UIColor whiteColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 0;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"Label的默认内容";
        label.font = [UIFont systemFontOfSize:12];
        
        [self.contentView addSubview:label];
        
        label;
    });
    self.titleLabel = titleLabel;
    
    UILabel * subTitleLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        //        label.backgroundColor = [UIColor whiteColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 0;
        label.textColor = [SNUtils colorWithString:@"7d7c82"];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"Label的默认内容";
        label.font = [UIFont systemFontOfSize:9];
        
        [self.contentView addSubview:label];
        
        label;
    });
    self.subTitleLabel = subTitleLabel;
    
    
    
    UIImageView * iconImageView = ({
        UIImageView * imageView = [[UIImageView alloc] init];
//                imageView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:1.0];
        
        [self.contentView addSubview:imageView];
        
        imageView;
    });
    self.iconImageView = iconImageView;
    
    UIButton * deleteButton = ({
        UIButton * button = [[UIButton alloc] init];
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"圆角矩形 1 拷贝 2"] forState:UIControlStateNormal];
        
        // [button setImage:[UIImage imageNamed:@"normalImage"] forState:UIControlStateNormal];
        // [button setImage:[UIImage imageNamed:@"selectedImage"] forState:UIControlStateSelected];
        
        [button addTarget:self action:@selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [self.contentView addSubview:button];
        
        
        
        button;
    });
    
    self.deleteButton = deleteButton;
}
- (void)setupUI
{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker * make) {
        //        make.left.mas_equalTo(self.contentView.mas_left).offset(0);
        //        make.right.mas_equalTo(self.contentView.mas_right).offset(0);
        make.centerX.mas_equalTo(self.contentView.mas_centerX).offset(0);
        make.top.mas_equalTo(self.contentView.mas_top).offset(15);
        //        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(0);
        
        //        make.width.mas_equalTo(50);
        //        make.height.mas_equalTo(50);
    }];
    
    [self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker * make) {
        //        make.left.mas_equalTo(self.contentView.mas_left).offset(0);
        //        make.right.mas_equalTo(self.contentView.mas_right).offset(0);
        make.centerX.mas_equalTo(self.contentView.mas_centerX).offset(0);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(6);
        //        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(0);
        
        //        make.width.mas_equalTo(50);
        //        make.height.mas_equalTo(50);
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker * make) {
        //        make.left.mas_equalTo(self.contentView.mas_left).offset(0);
        //        make.right.mas_equalTo(self.contentView.mas_right).offset(0);
        make.centerX.mas_equalTo(self.contentView.mas_centerX).offset(0);
        //        make.top.mas_equalTo(self.contentView.mas_top).offset(0);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-15);
        
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
        
    }];
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker * make) {
        
        make.right.mas_equalTo(self.contentView.mas_right).offset(-5);
        
        make.top.mas_equalTo(self.contentView.mas_top).offset(5);
        
        
        make.width.mas_equalTo(6.5);
        make.height.mas_equalTo(6.5);
        
    }];
}

- (void)config:(DBModel *)model indexPath:(NSIndexPath *)indexPath{
    _model = model;
    _indexPath = indexPath;
    
    self.titleLabel.text = model.title;
    self.subTitleLabel.text = model.subTitle;
    self.titleLabel.textColor = [SNUtils colorWithString:model.titleColorString];
//    self.subTitleLabel.textColor = [SNUtils colorWithString:model.titleColorString];
    self.iconImageView.image = [UIImage imageNamed:model.iconName];
    
    
    [self.iconImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.iconImageView.image.size.width );
        make.height.mas_equalTo(self.iconImageView.image.size.height);
    }];
}
- (void)configDomain:(HomeAddModuleHomeconfigdomain *)domain indexPath:(NSIndexPath *)indexPath{
    _homeAddModuleHomeconfigdomain = domain;
    _indexPath = indexPath;
    
    self.titleLabel.text = domain.title;
    self.subTitleLabel.text = domain.subTitle;
    self.titleLabel.textColor = [SNUtils colorWithString:domain.titleColorString];
    self.iconImageView.image = [UIImage imageNamed:domain.sequence];
    
    
    [self.iconImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.iconImageView.image.size.width );
        make.height.mas_equalTo(self.iconImageView.image.size.height);
    }];
}

- (void)deleteButtonClicked:(UIButton *)sender {
    NSLog(@"%@ | %s",self.class,__func__);
//    !self.deleteBlock ?: self.deleteBlock();
    if ([self.delegate respondsToSelector:@selector(deleteItem:)]) {
        [self.delegate deleteItem:self.indexPath];
    }
}

@end
